package com.jiazhong.jdbc.pojo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.util.List;

/**
 * @Auther:shuXin
 */
@SpringJUnitConfig(locations = "classpath:bean.xml")
@Component
public class JDBCTemplateTest {
    //传递一个对象，装载的对象
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testUpdate() {
        //添加的功能
        String sql = "insert into `t_emp`  values (null,?,?,?)";
        int update = jdbcTemplate.update(sql, "王五", 35, "男");
        System.out.println(update);

        //修改的功能
        /*String sql = "update `t_emp` set `name` = ? where `id` = ?";
        int update = jdbcTemplate.update(sql, "李四",1);
        System.out.println(update);*/

        //删除的功能
        /*String sql = "delete from t_emp where id = ?";
        int update = jdbcTemplate.update(sql, 2);
        System.out.println(update);*/
    }

    @Test
    public void testQuery() {
        String sql = "select * from t_emp where id = ?";
        Emp emp = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Emp.class), 1);
        System.out.println(emp);
    }

    //查询所有的数据并用集合来进行存储
    @Test
    public void TestQueryList() {
        String sql = "select * from t_emp";
        List<Emp> emp = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Emp.class));
        System.out.println(emp);
    }

    //查询返回单个的值
    @Test
    public void testQueryCount() {
        String sql = "select count(id) from t_emp";
        Integer i = jdbcTemplate.queryForObject(sql, Integer.class);
        System.out.println(i);
    }
}
